//
//  UITextFieldViewController.m
//  UITest
//
//  Created by wp w on 12-6-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UITextFieldViewController.h"
@implementation UITextFieldViewController

UITextField *myTextField;
UITextField *myTextField1;
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
        
    CGRect textRect = CGRectMake(10,10,300,31);
    myTextField = [[UITextField alloc] initWithFrame:textRect];
    myTextField.borderStyle = UITextBorderStyleRoundedRect; //外观，在此为圆角矩形
    myTextField.backgroundColor = [UIColor redColor];   //背景色
    
    myTextField.font = [UIFont systemFontOfSize:22.0];  //字体大小
    myTextField.adjustsFontSizeToFitWidth = YES;    //当文字放不下时，自动缩小
    myTextField.minimumFontSize = 2.0;  //可缩放的最小文字
    
    myTextField.clearButtonMode = UITextFieldViewModeWhileEditing;  //编辑时，在控件中出现删除所有按钮
    myTextField.keyboardType = UIKeyboardTypeDefault;
    
    myTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    myTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;  //首单词自动大小写
    myTextField.returnKeyType = UIReturnKeyDone;    //改变return为done
    
    //myTextField.secureTextEntry = YES;    //加密形式
    
    myTextField.delegate = self;
    
    [self.view addSubview:myTextField];
    
    //force keyboard to show immediately
    [myTextField becomeFirstResponder];     //进入界面后，立即显示键盘
    
    
    //一个自定义的textField，左边是一个label，右边是一个btn   
    CGRect textRect1 = CGRectMake(10,50,300,30);
    myTextField1 = [[UITextField alloc] initWithFrame:textRect1];
    myTextField1.borderStyle = UITextBorderStyleBezel; //外观
    myTextField1.backgroundColor = [UIColor blueColor];   //背景色
    
    myTextField1.font = [UIFont systemFontOfSize:22.0];  //字体大小
    myTextField1.adjustsFontSizeToFitWidth = YES;    //当文字放不下时，自动缩小
    myTextField1.minimumFontSize = 1.0;  //可缩放的最小文字
    
    //左边是一个label
    UILabel *paddingView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    paddingView.text = @"姓名:";
    paddingView.textColor = [UIColor darkGrayColor];
    paddingView.backgroundColor = [UIColor clearColor];
    //添加到textfield
    myTextField1.leftView = paddingView;
    myTextField1.leftViewMode = UITextFieldViewModeAlways;
    [paddingView release];
    
    //右边是一个btn
    UIButton *roundedRectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	
	CGRect buttonRect = CGRectMake(0, 0, 100, 30);
	[roundedRectButton setFrame:buttonRect];
	
	[roundedRectButton setTitle:@"Normal" forState:UIControlStateNormal];
	[roundedRectButton setTitle:@"Highlighted" forState:UIControlStateHighlighted];
	roundedRectButton.showsTouchWhenHighlighted = YES;
	
    [roundedRectButton addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchDown];
    [roundedRectButton addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpInside];
    myTextField1.rightView = roundedRectButton;
    myTextField1.rightViewMode = UITextFieldViewModeAlways;
    
    [self.view addSubview:myTextField1];  
    
    //隐藏键盘的另一种方式，但是出问题了，没有响应
    //在rootview中也没有问题
    //难道只是在tabview和navigation结合的情况下的里边的页面有问题？
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard:)];
    tapGestureRecognizer.numberOfTapsRequired = 1;
    tapGestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:tapGestureRecognizer];
    [tapGestureRecognizer release];
}

-(void)buttonDown:(id)sender
{
	NSLog(@"Button pushed down");
}

-(void)buttonRelease:(id)sender
{
	NSLog(@"Button released");
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    /* 
    //只能输入数字
    //limit text field to numeric values only
    NSCharacterSet *numberSet = [NSCharacterSet decimalDigitCharacterSet];
    for (NSUInteger i=0; i<[string length]; i++)
    {
        unichar ch = [string characterAtIndex:i];
        if (![numberSet characterIsMember:ch])
            return NO;
    }
    return YES;
    */
     
    
    //limit text field to 10 chars
    //最多输入十个字符
    int MAX_CHARS = 10;
    NSMutableString *newText = [NSMutableString stringWithString:textField.text];
    [newText replaceCharactersInRange:range withString:string];
    return ([newText length] <= MAX_CHARS);
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [myTextField release];	
    [super dealloc];
}

//在tabview中，该函数不会响应
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [myTextField resignFirstResponder];
    [myTextField resignFirstResponder];
}


//隐藏键盘
//1.设置键盘属性done
//2.使用touchesBegan，很多时候会捕捉不到鼠标事件，此时可以在viewdid中设置view颜色（view使用了）
//3.使用手势
- (void)hideKeyBoard:(id)sender
{
    [myTextField resignFirstResponder];
    [myTextField resignFirstResponder];
}


//- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
//- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

@end
